/*
** x_error.c for ls in /home/adine_g//xlib
** 
** Made by geoffrey adine
** Login   <adine_g@epitech.net>
** 
** Started on  Tue Jun  7 13:26:38 2011 geoffrey adine
** Last update Tue Jul  5 11:21:19 2011 michael guidet
*/

#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "server.h"

void	aff_err(char *str)
{
  fprintf(stderr, "%s\n", str);
  exit(EXIT_FAILURE);
}

void	*x_null(void *err, void *res, char *str)
{
  char	*file;
  int	line;

  line = __LINE__;
  file = __FILE__;
  if (res == err)
    {
      fprintf(stderr, "%s error (%s, %d): %s\n",
	      str, file, line, strerror(errno));
      exit(EXIT_FAILURE);
    }
  return (res);
}

int	x_int(int err, int res, char *str)
{
  char	*file;
  int	line;

  file = __FILE__;
  line = __LINE__;
  if (res == err)
    {
      fprintf(stderr, "%s error (%s, %d): %s\n",
	      str, file, line, strerror(errno));
      exit(EXIT_FAILURE);
    }
  return (res);
}

void	xstrcat(char **s1, char *s2, int *current_len)
{
  int	new_len;

  new_len = (*current_len);
  while (new_len < (int)(strlen((*s1)) + strlen(s2)))
    new_len += BUF_SIZE;
  if (new_len != *current_len)
    {
      (*s1) = x_null(NULL, realloc((*s1), sizeof(char) *
			       (new_len + 1)), "realloc");
      memset(&((*s1)[(*current_len)]), 0, new_len - (*current_len) + 1);
      (*current_len) = new_len;
    }
  x_null(NULL, strcat((*s1), s2), "strcat");
}
